package com.android.hedgehog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoresController;
import com.scoreloop.client.android.core.controller.UserController;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.ScoreFormatter;
import com.scoreloop.client.android.core.model.Session;
import com.scoreloop.client.android.core.model.User;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class LeaderBoard2 extends Activity {
	private final static int DIALOG_ERROR = 0;
	private final static int DIALOG_PROGRESS = 1;
	Map<String, Object> userContext = new HashMap<String, Object>();// added
	String name = "";

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.leaderboard2);
	}

	public void onResume() {
		super.onResume();

		final RequestControllerObserver observer = new RequestControllerObserver() {

			@Override
			public void requestControllerDidReceiveResponse(
					final RequestController requestController) {
				Log.d("00000000", "REQUESTRECIEVED:  " + requestController);

				// Step 3
				// in the callback:
				User user = Session.getCurrentSession().getUser();
				Map<String, Object> userContext = new HashMap<String, Object>();
				Integer age=new Integer(0);
				String display="empty";

				// Step 4
				userContext = user.getContext();

				// Step 5
				if (!userContext.isEmpty()){
					age = (Integer) userContext.get("age");
					display= (String) userContext.get("username");
				}else{
					age=0;
					display="unknown";
				}
				// get the scores from our controller
				// access the User object

				final List<String> scoreList = new ArrayList<String>();
				scoreList.add("TRAIN "+display+ "  :  "+age);

				// find the list
				final ListView list = (ListView) findViewById(R.id.leaderboard_list);

				// set up an adapter for our list view
				final ListAdapter adapter = new ArrayAdapter<String>(LeaderBoard2.this, android.R.layout.simple_list_item_1, scoreList);

				// put the adapter into the list
				list.setAdapter(adapter);
				/*
				 * // set up the leaderboard as string array final List<String>
				 * scoreList = new ArrayList<String>();
				 * 
				 * // find the list final ListView list = (ListView)
				 * findViewById(R.id.leaderboard_list);
				 * 
				 * // set up an adapter for our list view final ListAdapter
				 * adapter = new ArrayAdapter<String>( LeaderBoard2.this,
				 * android.R.layout.simple_list_item_1, scoreList);
				 * 
				 * // put the adapter into the list list.setAdapter(adapter);
				 */

				// we're done!
				dismissDialog(DIALOG_PROGRESS);

			}

			@Override
			public void requestControllerDidFail(
					final RequestController aRequestController,
					final Exception anException) {
				Log.d("00000000", "REQUESTFAILED:  " + aRequestController);
				dismissDialog(DIALOG_PROGRESS);
				showDialog(DIALOG_ERROR);
			}
		};// RCO

		// Create an instance of UserController that uses the current session by
		// default
		UserController myUserController = new UserController(observer);
		Log.d("-------------", "11111111111");

		// Use the controller request info for the current session user
		myUserController.loadUserContext();
		Log.d("-------------", "22222222222");

		// It is a good idea to show a progress indicator while making the
		// server request
		showDialog(DIALOG_PROGRESS);
		Log.d("-------------", "333333333333333");

	}

	// handler to create our dialogs
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_PROGRESS :
				return ProgressDialog.show(LeaderBoard2.this, "",
						getString(R.string.loading));
			case DIALOG_ERROR :
				return (new AlertDialog.Builder(this))
						.setMessage(R.string.leaderboard_error)
						.setPositiveButton(R.string.too_bad, null).create();
		}
		return null;
	}

}
